#include <linux/module.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/mm.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/cdev.h>
#include <asm/io.h>
#include <asm/system.h>
#include <asm/uaccess.h>

#define GLOBALMEM_SIZE 0x1000 
#define GLOBALMEM_MAJOR 280

#define GLOBALMEM_MAGIC 0x2F
#define MEM_CLEAR _IO(GLOBALMEM_MAGIC, 0)

static int globalmem_major = GLOBALMEM_MAJOR;

struct globalmem_dev {
	struct cdev cdev;
	unsigned char mem[GLOBALMEM_SIZE]; //global memory
	struct semaphore sem;
};

// struct globalmem_dev dev; 
struct globalmem_dev *globalmem_devp; //device structure pointer

int globalmem_open(struct inode *inode, struct file *filp)
{
	struct globalmem_dev *dev;
	
	dev = container_of(inode->i_cdev, struct globalmem_dev, cdev);	
	filp->private_data = dev;
	return 0;
}

int globalmem_release(struct inode *inode, struct file *filp)
{
	return 0;
}

static ssize_t globalmem_read(struct file *filp, char __user *buf, size_t count, loff_t *ppos)
{
	unsigned long p = *ppos;
	int ret = 0;

	struct globalmem_dev *dev = filp->private_data;

	if (p >= GLOBALMEM_SIZE) 
		return 0;
	if (count > GLOBALMEM_SIZE - p)
		count = GLOBALMEM_SIZE - p;

	printk(KERN_ALERT "read try to get sem-----------------------------\n");
	if (down_interruptible(&dev->sem))
		return - ERESTARTSYS;
	printk(KERN_ALERT "read have gotten sem--------------\n");

	if (copy_to_user(buf, (void*)(dev->mem + p), count))
		ret = - EFAULT;
	else {
		*ppos += count;
		ret = count;

		printk(KERN_ALERT "read %d bytes(s) from %d\n", count, (int)p);
	}

	up(&dev->sem);
	printk(KERN_ALERT "read release sem\n");

	return ret;
}

static ssize_t globalmem_write(struct file *filp, const char __user *buf, size_t count, loff_t *ppos)
{
	unsigned long p = *ppos;
	int ret = 0;

	struct globalmem_dev *dev = filp->private_data;
	
	if (p >= GLOBALMEM_SIZE)
		return 0;
	if (count > GLOBALMEM_SIZE - p)
	count = GLOBALMEM_SIZE - p;

	printk(KERN_ALERT "write try to get sem------------------------------------\n");
	if (down_interruptible(&dev->sem))
		return - ERESTARTSYS;
	printk(KERN_ALERT "write had gotten sem----------------\n");
	
	printk(KERN_ALERT "sleep 1s\n");
	msleep(1000);

	if (copy_from_user(dev->mem + p, buf, count))
		ret = - EFAULT;
	else {
		*ppos += count;
		ret = count;

		printk(KERN_ALERT "written %d bytes(s) from %d\n", count, (int)p);
	}

	up(&dev->sem);
	printk(KERN_ALERT "write release sem\n");
	
	return ret;
}

static loff_t globalmem_llseek(struct file *filp, loff_t offset, int orig)
{
	loff_t ret;
	switch (orig) {
	case 0: /*begin seek at the beginning of file*/
		if (offset < 0) {
			ret = - EINVAL;
			break;
		}
		if ((unsigned int)offset > GLOBALMEM_SIZE) {
			ret = - EINVAL;
			break;
		}
		filp->f_pos = (unsigned int) offset;
		ret = filp->f_pos;
		break;
	case 1: /*begin seek at the current location*/
		if ((filp->f_pos + offset) > GLOBALMEM_SIZE) {
			ret = -EINVAL;
			break;
		}
		if ((filp->f_pos + offset) < 0) {
			ret = - EINVAL;
			break;
		}
		filp->f_pos += offset;
		ret = filp->f_pos;
		break;
	default:
		ret = - EINVAL;
	}
		
	return ret;
}

static int globalmem_ioctl(struct inode *inodep, struct file *filp, unsigned int cmd, unsigned long arg)
{
	struct globalmem_dev *dev = filp->private_data;	

	switch (cmd) {
	case MEM_CLEAR:

		printk(KERN_ALERT "MEM_CLEAR try to get sem.\n");
		if (down_interruptible(&dev->sem))
			return - ERESTARTSYS;
		printk(KERN_ALERT "MEM_CLEAR had gotten sem.\n");
		
		memset(dev->mem, 0, GLOBALMEM_SIZE);

		up(&dev->sem);
		printk(KERN_ALERT "MEM_CLEAR release sem.\n");
		
		printk(KERN_ALERT "globalmem is set to zero\n");
		break;
	default:
		return - EINVAL;
	}

	return 0;
}

static const struct file_operations globalmem_fops = {
	.owner = THIS_MODULE,
	.llseek = globalmem_llseek,
	.read = globalmem_read,
	.write = globalmem_write,
	.ioctl = globalmem_ioctl,
	.open = globalmem_open,
	.release = globalmem_release,
};

static void globalmem_setup_cdev(struct globalmem_dev *dev, int index)
{
	int err, devno = MKDEV(globalmem_major, index);
	
	cdev_init(&dev->cdev, &globalmem_fops);
	dev->cdev.owner = THIS_MODULE;
	err = cdev_add(&dev->cdev, devno , 1);
	if (err)
		printk(KERN_ALERT "Error %d adding globalmem %d", err, index);
}

int globalmem_init(void)
{
	int result;
	dev_t devno = MKDEV(globalmem_major, 0);
	
	printk(KERN_ALERT "globalmem init.\n");

	if (globalmem_major)
		result = register_chrdev_region(devno, 2, "globalmem");
	else {
		result = alloc_chrdev_region(&devno, 0, 2, "globalmem");
		globalmem_major = MAJOR(devno);
	}
	if (result < 0)
		return result;

	globalmem_devp = kmalloc(2 * sizeof(struct globalmem_dev), GFP_KERNEL);
	if (!globalmem_devp) {
		result = - ENOMEM;
		goto fail_malloc;
	}

	memset(globalmem_devp, 0, 2 * sizeof(struct globalmem_dev));

	globalmem_setup_cdev((struct globalmem_dev *)&(globalmem_devp[0].cdev), 0);
	globalmem_setup_cdev((struct globalmem_dev *)&(globalmem_devp[1].cdev), 1);

	init_MUTEX(&globalmem_devp->sem);

	printk(KERN_ALERT "return 0\n");
	return 0;

fail_malloc:
	unregister_chrdev_region(devno, 1);
	return result;
}

void globalmem_exit(void)
{
	cdev_del(&(globalmem_devp[0].cdev));
	cdev_del(&(globalmem_devp[1].cdev));

	kfree(globalmem_devp);
	unregister_chrdev_region(MKDEV(globalmem_major, 0), 2);
}

module_init(globalmem_init);
module_exit(globalmem_exit);

//MODULE_AUTHOR("jiang <jc@163.com>");
//MODULE_LICENSE("Dual BSD/GPL");
//MODULE_DESCRIPTION("allocate global memory");
//MODULE_ALIAS("globalmem info.");

